package com.cnsugar.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;

import com.cnsugar.ai.face.FaceHelper;
import com.cnsugar.ai.face.SeetafaceBuilder;
import com.cnsugar.ai.face.bean.Result;

public class testRegister {

	public static void main(String[] args) throws Exception  {
		// TODO Auto-generated method stub
		testRegister test = new testRegister();
		test.testSearch();

	}
	
	public void testCompare() throws Exception {
        String img1 = "F:\\ai\\basehead.jpg";
        String img2 = "F:\\ai\\head.jpg";
        System.out.println("result:"+FaceHelper.compare(new File(img1), new File(img2)));
    }
   
      public static ArrayList<File> getListFiles(Object obj) {  
        File directory = null;  
        if (obj instanceof File) {  
            directory = (File) obj;  
        } else {  
            directory = new File(obj.toString());  
        }  
        ArrayList<File> files = new ArrayList<File>();  
        if (directory.isFile()) {  
            files.add(directory);  
            return files;  
        } else if (directory.isDirectory()) {  
            File[] fileArr = directory.listFiles();  
            for (int i = 0; i < fileArr.length; i++) {  
                File fileOne = fileArr[i];  
                files.addAll(getListFiles(fileOne));  
            }  
        }  
        return files;  
    }  
   
//    public void addPic() throws IOException {
//    
//      String imagePath = "F:\\ai\\basehead.jpg";
//      FileInputStream f = new FileInputStream(imagePath);  
//      BufferedImage image = ImageIO.read(f); 
//      String key = UUID.randomUUID().toString().replace("-", "");
//       System.out.println("照片："+key);
//      FaceHelper.register(key, image);
//    }
    
      public void addPic() throws Exception {
//      String imagePath = "F:\\ai\\basehead.jpg";
      String imagePath = "H:\\aipic\\base";
      ArrayList<File> files=getListFiles(imagePath);  
      for(int i=0;i<files.size();i++) {
       File file = files.get(i);
       String name = file.getName();
       String prefix=name.substring(name.lastIndexOf("."));
       int num=prefix.length();//得到后缀名长度
      String fileOtherName=name.substring(0, name.length()-num);//得到文件名。去掉了后
      
       System.out.println("照片名："+name);
      FileInputStream f = new FileInputStream(file);  
      BufferedImage image = ImageIO.read(f); 
      String key = UUID.randomUUID().toString().replace("-", "");
       System.out.println("照片："+fileOtherName);
      FaceHelper.register(fileOtherName, image);
      Thread.currentThread().sleep(1000);
      }
    }

    public void testSearch() throws IOException {
       SeetafaceBuilder.build();//系统启动时先调用初始化方法
        //等待初始化完成
        while (SeetafaceBuilder.getFaceDbStatus() == SeetafaceBuilder.FacedbStatus.LOADING || SeetafaceBuilder.getFaceDbStatus() == SeetafaceBuilder.FacedbStatus.READY) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        long l = System.currentTimeMillis();
        Result result = FaceHelper.search(FileUtils.readFileToByteArray(new File("H:\\aipic\\1.jpg")));
        System.out.println("搜索结果：" + result + "， 耗时：" + (System.currentTimeMillis() - l));
    }
}
